package HLLib.userControl.HLPanelPickUp;

import javax.microedition.lcdui.Image;

import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLImage;
import HLLib.base.HLString;
import HLLib.control.HLPanel;
import HLLib.control.HLPhotoFrame;
import HLLib.handfere.HLUIRes;
import HLLib.handfere.HLCommunityData.HLGame.HLGame;

public class HLGamePanel extends HLPanel {
    public static final int PHOTO_X = 3;

    public static final int PHOTO_Y = 5;

    public static final int PHOTO_TO_TEXT = 10;

    public static final int TEXT_Y = 2;

    public HLGame game;

    HLPhotoFrame photoFrame;

    int xText;

    // HLTextPage textPage ;
    public int selectColor = 0xffff00;

    public int color = 0xffecf3fd;

    Image img;

    Image rongyuImage[] = HLUIRes.uires.GetGold();

    public HLGamePanel(HLGame game, Image img) {
        this.game = game;
        this.width = (short) HLLib.base.HLScreen.width;
        this.img = img;
        tabelStop = true;
        photoFrame = new HLPhotoFrame(game.icon);
        photoFrame.x = PHOTO_X;
        photoFrame.y = PHOTO_Y;

        Add(photoFrame);

        xText = PHOTO_X + photoFrame.width + PHOTO_TO_TEXT;
        this.height = Math.max(TEXT_Y + HLGraphics.StringHeight() + TEXT_Y + HLGraphics.StringHeight() + 2, photoFrame.height + PHOTO_Y * 2);
        needDownload = game.icon.GetImage1() == null;
        oriHeight = game.icon.GetHeight();
    }

    int oriHeight = 0;

    boolean haveDownloadOver = false;

    boolean needDownload = true;

    public void AdjustSize() {
        if (parent != null) {
            parent.AdjustSize();
        }
    }

    public void Logic() {

        if (needDownload) {
            if (game.icon != null && !haveDownloadOver) {
                if (game.icon.GetImage1() == null) {
                    haveDownloadOver = false;
                } else {
                    haveDownloadOver = true;
                    this.height += game.icon.GetHeight() - oriHeight;
                    AdjustSize();
                    GetUiRoot().Refresh();
                }
            }
        }

        super.Logic();
        if (IsFocus()) {
            RefreshEvent(GetScreenX(), GetScreenY(), GAME_BUTTON_ACTION);
        }
    }

    public void Refresh() {
        super.Refresh();

    }

    public void Render(HLGraphics g) {
        // super.Render(g);
        int screenX = GetScreenX();
        int screenY = GetScreenY();
        g.FillRect(screenX, screenY, width, height, 0xffffff);
        if (IsFocus()) {
            // g.DrawImageFillRect(new HLImage(img[1]), screenX, screenY, width,
            // img[1].getHeight(), HLGraphics_H.TRANS_NONE);
            // g.DrawImageFillRect(new HLImage(img[0]), screenX, screenY +
            // height - img[0].getHeight(), width, img[1].getHeight(),
            // HLGraphics_H.TRANS_NONE);
            g.FillRect(screenX, screenY, width, height, HLPanelGroup.SELECTED_COLOR);// d0d9f2);//0xd0ebf2);
        } else {
        }
        int achivementWidth = 0;
        int achivementIconWidth = 0;
        int space = 4;

        if (game.achivement >= 0) {
            achivementWidth = HLGraphics.StringWidth1("123");
            achivementIconWidth = rongyuImage[0].getWidth();
            g.DrawStringAdjustable(new HLString(game.achivement + ""), 0, 0, screenX, screenY, width - space, height, TEXT_STYLE_NORMAL, RIGHT_VCENTER);
            g.DrawImage(new HLImage(rongyuImage[0]), screenX + width - space - achivementWidth - achivementIconWidth, screenY + (height - rongyuImage[0].getHeight() >> 1), HLGraphics_H.TRANS_NONE);
        }

        xText = PHOTO_X + photoFrame.width + PHOTO_TO_TEXT;
        int offsetX = screenX + xText;
        int offsetY = screenY + TEXT_Y;
        int nameWidth = HLGraphics.StringWidth1(game.name);
        g.DrawString(new HLString(game.name), offsetX + 3, offsetY, 0xff0000ff, 0, TEXT_STYLE_NORMAL);
        // g.DrawString(new HLString("(" + game.version + ")"), offsetX + 3 +
        // nameWidth, offsetY, 0xff4b4b4b, 0, TEXT_STYLE_NORMAL);
        if (game.version != null && game.achivement < 0) {
            g.DrawStringAdjustable(new HLString(game.version), 0xff4b4b4b, 0, 0, offsetY, width - space, height, TEXT_STYLE_NORMAL, RIGHT_TOP);
        }

        int stringHeight = HLGraphics.StringHeight();
        offsetY += stringHeight + TEXT_Y;

        g.ClipRect(offsetX, offsetY, width - offsetX - achivementWidth - space * 2 - achivementIconWidth, stringHeight);
        g.DrawString(new HLString(game.summary), offsetX, offsetY, 0xff4b4b4b, 0, TEXT_STYLE_NORMAL);
        g.SetClip(0, 0, HLLib.base.HLScreen.width, HLLib.base.HLScreen.height);

        photoFrame.Render(g);

    }

}
